﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class sqlFieldEdit : System.Web.UI.Page
{
    public string dbName = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        dbName = Request.QueryString["dbname"];
        if (dbName == "")
        {
            iData.mysqlDataAcess worker = new iData.mysqlDataAcess();
            dbName = worker.dbSetting.database;
        }
        if (!IsPostBack)
        {
            string tblName = Request["tblName"];
            loadData(tblName);
        }

    }


    private void loadData(string tblname)
    {
        string colname = Request["colname"];
        string tblTitle = Request["memo"];

        lblTableName.Text = tblname;
        txtTblName.Text = tblname;
        lblFieldName.Text = colname;
        txtFieldName.Text = colname;

        if (tblTitle != "")
        {

            txtTblTitle.Text = tblTitle;
        }
    }


    private void setFieldMemo(string tblname, string colName, string memo)
    {
 
        //sql = sql.Replace("appDataPort", tblname);
        iData.mysqlDataAcess worker = new iData.mysqlDataAcess();
       string sql = @"select COLUMN_TYPE as typename from information_schema.COLUMNS
where table_schema='chromosome_db' and TABLE_NAME='taskdetail' and COLUMN_NAME='colsname';
";
        sql = sql.Replace("taskdetail", tblname).Replace("colsname", colName);
        if (dbName != "")
        {
            sql = sql.Replace("chromosome_db", dbName);
        }
        else
        {
            sql = sql.Replace("chromosome_db", worker.dbSetting.database);

        }
        string typename = worker.ExecuteScalar(sql);
        //int rowsCount = worker.ExecuteNonQuery(sql);

        sql = "ALTER TABLE "+dbName+"." + tblname + " MODIFY COLUMN `" + colName + "`   "+ typename+" COMMENT '" + memo + "';";
        //sql = sql.Replace("appDataPort", tblname);
        //Response.Write(sql);Response.End();
        worker.ExecuteNonQuery(sql);

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string tblTitle = txtTblTitle.Text;
        string tblName = txtTblName.Text;
        string colName = txtFieldName.Text;
        setFieldMemo(tblName, colName, tblTitle);
        LIB.JsWrite.alertAndCloseUpdateParent("修改成功");
    }
}